//! Yaya - 基于libOS架构的轻量级操作系统库
//! 
//! 将操作系统功能作为库直接链接到应用中，实现直接硬件访问和最高性能。

#![cfg_attr(feature = "no_std", no_std)]

#[macro_use]
extern crate log;

pub mod prelude;

// 硬件抽象层
pub mod hal {
    pub mod cpu;
    pub mod memory;
    pub mod devices;
    pub mod interrupts;
}

// 系统服务
pub mod services {
    pub mod scheduler;
    pub mod allocator;
    pub mod fs;
    pub mod network;
}

// 应用框架
pub mod app {
    pub mod runtime;
    pub mod manager;
    pub mod context;
}

// 工具和工具函数
pub mod utils {
    pub mod sync;
    pub mod error;
    pub mod macros;
}

use crate::app::runtime::YayaRuntime;
use crate::utils::error::YayaError;

/// Yaya库的主入口点
pub struct Yaya;

impl Yaya {
    /// 创建新的Yaya运行时实例
    pub fn new() -> Result<YayaRuntime, YayaError> {
        YayaRuntime::new()
    }
    
    /// 初始化Yaya库
    pub fn init() -> Result<(), YayaError> {
        info!("Yaya libOS初始化开始");
        
        // 初始化硬件抽象层
        hal::cpu::init()?;
        hal::memory::init()?;
        hal::interrupts::init()?;
        
        info!("Yaya libOS初始化完成");
        Ok(())
    }
    
    /// 关闭Yaya库
    pub fn shutdown() -> Result<(), YayaError> {
        info!("Yaya libOS关闭开始");
        
        // 清理资源
        hal::interrupts::shutdown()?;
        hal::memory::shutdown()?;
        
        info!("Yaya libOS关闭完成");
        Ok(())
    }
}

/// 预导入模块，包含常用的类型和特性
pub mod prelude {
    pub use crate::Yaya;
    pub use crate::utils::error::{YayaError, Result};
    pub use crate::app::runtime::YayaRuntime;
    pub use crate::hal::cpu::CpuManager;
    pub use crate::hal::memory::MemoryManager;
    pub use crate::services::scheduler::Scheduler;
    
    // 重新导出常用的标准库类型
    pub use core::fmt;
    pub use alloc::vec::Vec;
    pub use alloc::string::String;
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_yaya_init() {
        assert!(Yaya::init().is_ok());
        assert!(Yaya::shutdown().is_ok());
    }
}